package org.example.day20240909.view;

import org.example.day20240909.dao.DepartmentDao;
import org.example.day20240909.dao.EmployeeDao;
import org.example.day20240909.model.Department;
import org.example.day20240909.model.Employee;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class EmployeeView {

    public void displayList(List<Employee> employeeList){
        System.out.println("员工编号" + "\t\t\t" + "姓名" + "\t\t" + "性别" + "\t\t"
                + "生日" + "\t\t\t" + "入司日期" + "\t\t" + "当前部门" + "\t" + "状态");
        for (Employee employee : employeeList) {
            System.out.print(employee.getEmployee_id() + "\t");
            System.out.print(employee.getEmployee_name() + "\t\t");
            System.out.print(employee.getGender() + "\t\t");
            System.out.print(employee.getBirthday() + "\t");
            System.out.print(employee.getHire_date() + "\t");
            System.out.print(employee.getDepartment_name() + "\t\t");
            System.out.print(employee.getState() + "\n");
        }
    }
    public void display(Employee employee){
        System.out.println("员工编号" + "\t\t\t" + "姓名" + "\t\t" + "性别" + "\t\t" + "生日"
                + "\t\t\t" + "入司日期" + "\t\t" + "当前部门" + "\t" + "状态");
        System.out.print(employee.getEmployee_id() + "\t");
        System.out.print(employee.getEmployee_name() + "\t\t");
        System.out.print(employee.getGender() + "\t\t");
        System.out.print(employee.getBirthday() + "\t");
        System.out.print(employee.getJoin_date() + "\t");
        System.out.print(employee.getDepartment_name() + "\t\t");
        System.out.print(employee.getState() + "\n");
    }

    public void checkEmployeeList() {
        EmployeeDao employeeDao = new EmployeeDao();
        List<Employee> employeeList = employeeDao.checkEmployeeList();
        displayList(employeeList);
    }

    public void searchEmployee(String inputId) {
        EmployeeDao employeeDao = new EmployeeDao();
        Map<Employee, Map<String,String>> employeeMaps = employeeDao.searchEmployeeMsg(inputId);
        for (Map.Entry<Employee, Map<String,String>> entry : employeeMaps.entrySet()){
            System.out.println("员工编号：" + entry.getKey().getEmployee_id());
            System.out.println("员工名字：" + entry.getKey().getDepartment_name());
            System.out.println("员工邮箱：" + entry.getKey().getEmployee_email());
            System.out.println("性别：" + entry.getKey().getGender());
            System.out.println("员工生日：" + entry.getKey().getBirthday());
            System.out.println("年龄：" + entry.getKey().getAge());
            System.out.println("参加工作日期：" + entry.getKey().getJoin_date());
            System.out.println("工龄：" + entry.getKey().getWork_experience());
            System.out.println("入司日期：" + entry.getKey().getHire_date());
            System.out.println("司龄：" + entry.getKey().getCompany_experience());
            System.out.println("所属部门：" + entry.getKey().getDepartment_name());
            System.out.println("履历：");
            Map<String,String> turnMaps = entry.getValue();
            for (Map.Entry<String,String> turnEntry : turnMaps.entrySet()){
                System.out.println(turnEntry.getKey()+" "+turnEntry.getValue());
            }
        }

    }

    public Employee getEmployee(String inputId) {
        EmployeeDao employeeDao = new EmployeeDao();
        Employee employee = employeeDao.searchEmployee(inputId);
        display(employee);
        return employee;
    }

    public boolean checkEmployeeId(String inputId) {
        EmployeeDao employeeDao = new EmployeeDao();
        return employeeDao.checkEmployeeId(inputId);
    }

    public Employee checkDepartmentEmployeeId(String inputId) {
        EmployeeDao employeeDao = new EmployeeDao();
        Employee employee = employeeDao.searchEmployee(inputId);
        System.out.println("员工编号" + "\t\t\t" + "姓名" + "\t" + "当前部门");
        System.out.print(employee.getEmployee_id() + "\t");
        System.out.print(employee.getEmployee_name() + "\t");
        System.out.print(employee.getDepartment_name()+"\n");
        return employee;
    }


    public boolean checkEmployeeEmail(String inputEmail) {
        EmployeeDao employeeDao = new EmployeeDao();
        return employeeDao.checkEmployeeEmail(inputEmail);
    }

    public void addEmployee(Employee employee) {
        EmployeeDao employeeDao = new EmployeeDao();
        employeeDao.addEmployee(employee);
    }

    public void deleteEmployee(String inputId) {
        EmployeeDao employeeDao = new EmployeeDao();
        employeeDao.deleteEmployee(inputId);
    }

    public void updateEmployee(Employee employee) {
        EmployeeDao employeeDao = new EmployeeDao();
        employeeDao.updateEmployee(employee);
    }

    public void turnEmployee(Employee employee, String oldDepartment_name, String newDepartmentId) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String today = sdf.format(date);
        EmployeeDao employeeDao = new EmployeeDao();
        DepartmentDao departmentDao = new DepartmentDao();
        Department department = departmentDao.findEmployeeDepartmentId(oldDepartment_name);
        String oldDepartmentId = department.getDepartment_id();
        employeeDao.turnEmployee(employee, oldDepartmentId, newDepartmentId, today);
    }

    public Employee getEmployeeInJob(String inputDepartmentHeadId) {
        EmployeeDao employeeDao = new EmployeeDao();
        return employeeDao.getEmployeeInJob(inputDepartmentHeadId);
    }

    public void checkEmployeeHeadDepartment() {
        EmployeeDao employeeDao = new EmployeeDao();
        List<Employee> employeeList = employeeDao.checkEmployeeHeadDepartment();
        displayList(employeeList);
    }
}
